<?php
class hourGraphComponent extends sfComponent
{

  public function execute($request)
  {
    $c = aluCriteria::buildFromRequest($this->getRequest());
    $c->setPrimaryTableName(LogPeer::TABLE_NAME);
    $c->addAsColumn('nb', 'COUNT(*)');
    $c->addAsColumn('heure', sprintf('HOUR(%s)', LogPeer::DATE));
    $c->addGroupByColumn(sprintf('HOUR(%s)', LogPeer::DATE));

    $c1 = clone $c;
    $c1->add(LogPeer::TYPE, TypesPeer::TYPE_CONNEXION);

    $c2 = clone $c;
    $c2->add(LogPeer::TYPE, TypesPeer::TYPE_DECONNEXION);

    $this->series = array(
      array(
        'title'  => 'connexions',
        'data'   => $this->criteriaToTableByHour($c1),
      ),
      array(
        'title'  => 'déconnexions',
        'data'   => $this->criteriaToTableByHour($c2),
      ),
    );
  }

  public function criteriaToTableByHour(Criteria $c)
  {
    $tab = array();
    for($i = 0; $i<24; $i++)
    {
      $tab[] = 0;
    }
    $stmt = BasePeer::doSelect($c);
    while ($lr = $stmt->fetch(PDO::FETCH_ASSOC))
    {
      $tab[$lr['heure']] = $lr['nb'];
    }
    return $tab;
  }
}